﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Billing.Utilities;
using Billing.Dtos;
using System.Windows.Controls.Primitives;
using Billing.Controls;
using System.Data;
using System.ComponentModel;

namespace Billing.Windows
{
    public partial class AddUpdateItem : Window
    {
        public AddUpdateItem()
        {
            InitializeComponent();

            try
            {
                txtCode.Focus();
                cmbDept.ItemsSource = ((IListSource)Database.GetDepartments()).GetList();
                cmbDept.DisplayMemberPath = "DeptName";
                cmbDept.SelectedValuePath = "DeptID";
                cmbDept.SelectedIndex = 0;
            }
            catch (Exception) { }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Escape)
                {
                    this.Close();
                }
                else if (e.Key == Key.Enter)
                {

                }
            }
            catch (Exception) { }
        }

        private void txtCode_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    GetItem();
                }
            }
            catch (Exception) { }
        }

        private void lstLessStockShown_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void txtCode_TextChanged(object sender, TextChangedEventArgs e)
        {
            GetItem();
        }

        private void GetItem()
        {
            try
            {
                DataTable dt = Database.GetItem(txtCode.Text);
                if (dt != null && dt.Rows.Count > 0)
                {
                    //txtCode.IsReadOnly = true;

                    for (int cnt = 0; cnt < cmbDept.Items.Count; cnt++)
                    {
                        if ((cmbDept.Items[cnt] as DataRowView).Row.ItemArray[0].ToString() == dt.Rows[0]["Dept"].ToString())
                        {
                            cmbDept.SelectedIndex = cnt;
                            break;
                        }
                    }

                    txtCostPrice.Text = dt.Rows[0]["CostPrice"].ToString();
                    txtSellingPrice.Text = dt.Rows[0]["SellingPrice"].ToString();
                    if (dt.Rows[0]["AcceptNegativeStock"].ToString().ToLower() == "true")
                        lstAcceptNegative.SelectedIndex = 1;
                    else
                        lstAcceptNegative.SelectedIndex = 0;

                    if (dt.Rows[0]["AcceptDecimalStock"].ToString().ToLower() == "true")
                        lstAcceptDecimal.SelectedIndex = 1;
                    else
                        lstAcceptDecimal.SelectedIndex = 0;

                    txtPopupLimit.Text = dt.Rows[0]["PopupLimit"].ToString();

                    if (dt.Rows[0]["IsLessStockShown"].ToString().ToLower() == "true")
                        lstLessStockShown.SelectedIndex = 1;
                    else
                        lstLessStockShown.SelectedIndex = 0;
                }
                else
                {

                }
            }
            catch (Exception) { }
        }
    }
}